package com.sxt.test;

import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sxt.entity.Book;
import com.sxt.service.CalcImpl1;
import com.sxt.service.IBookList;
import com.sxt.service.ICalc;

public class Demo {
	/**
	 * 普通实现方法
	 */
	@Test
	public void test1() {
		System.out.println(new CalcImpl1().compute(1, 1));
	}
	
	/** 
	 * 使用spring获取bean
	 */
	@Test
	public void test2() {
		BeanFactory bf = new ClassPathXmlApplicationContext("applicationContext.xml");
		ICalc c = (ICalc) bf.getBean("calc");
		System.out.println(c.compute(5, 3));
	}
	
	/**
	 * 依赖注入
	 */
	@Test
	public void test3() {
		BeanFactory bf = new ClassPathXmlApplicationContext("applicationContext.xml");
		IBookList blist = (IBookList) bf.getBean("blist");
		Book b = blist.find("西游记");
		if (b != null) {
			System.out.println("《" + b.getName() +  "》的作者是" + b.getAuthor());
		} else {
			System.out.println("找不到");
		}
	}
}
